<?

function check_uploaded_files($files)
{
   $chkfiles = array();
   
   if(is_array($files) && count($files)>0)
   {
      foreach($files as $k => $f)
      {
         if( is_uploaded_file($files[$k]['tmp_name']) )
         {
	    $fname = preg_replace('/(^.*'._badextensions.'$)/', '${1}.txt', justwords($_FILES[$k]['name']));
	 
            $chkfiles[] = array( 'file' => $files[$k]['tmp_name'],
                                 'name' => $fname
                               );
         }
      }
   }
   
   return $chkfiles;
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------


function html_attachlist($cid, $editmode=false)
{
   $cid = justdigs($cid);
   $cdir = _atchdir.$cid;

   $files = listdir($cdir);
   $c = ($files) ? count($files) : false;

   $files_html = '';

   if( $c )
   {
      $files_tpl = ($editmode) ? get_template('attachment_edit.html') : get_template('attachment_view.html');

      $files_kwd = array( '_CMTID_',
                          '_COUNTER_',
                          '_NAME_',
                          '_SIZE_',
                          '_FULLNAME_'
                        );


      for($i=0; $i<$c; $i++)
      {
         $fname = (strlen($files[$i]['name']) <= 40) ? $files[$i]['name'] : trim( mb_substr($files[$i]['name'], 0, 40) ).'...';

         $r = array($cid, $i, $fname, $files[$i]['size'], $files[$i]['name']);

         $files_html .= str_replace($files_kwd, $r, $files_tpl);
      }
   }

   $all_tpl = ($editmode) ? get_template('attachments_edit.html') : ( ($c) ? get_template('attachments_view.html') : '');
   $all_kwd = array('_CID_', '_COUNT_', '_ATTACHMENTS_');

   $html = str_replace($all_kwd, array($cid, $c, $files_html), $all_tpl);

   return($html);
}

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

function upload_files($cid, $files) // very trustful function
{
   $c = ( is_array($files) ) ? count($files) : 0;
   
   if($cid>0 && $c>0)
   {
      $moved = '';
      
      $dir = _atchdir.$cid.'/';
      
      if( !is_dir($dir) )
      {
         if( ! mkdir($dir, 0777) ) return false; // if can't create new dir - leave this function
      }
      
      for($i=0; $i<$c; $i++)
      {
         if( move_uploaded_file($files[$i]['file'], $dir.$files[$i]['name']) )
         {
            $moved .= " '".$files[$i]['name']."' moved,\n ";
         }
      }
      
      return $moved;
   }
   else return false;
}


//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

function deletefiles($cid)
{
   $dir = _atchdir.$cid.'/';
   
   if( !is_dir($dir) ) return true; // dir's not exist, so, files are already deleted. ;)
   
   $files = listdir($dir);
   $c = count($files);
   
   if($c>0)
   {
      for($i=0; $i<$c; $i++)
      {
         $file = $dir.$files[$i]['name'];
         
         if(is_file($file)) unlink($file);
      }
   }
   
   rmdir($dir);
   
   return true;
}

























?>
